﻿'use strict';
QuotingToolApp.factory('contractsData', ['$resource','QUOTINGTOOL_CONFIG',
    function ($resource, QUOTINGTOOL_CONFIG) {        

        return {
            getContracts: function(searchParams) {               

                var resource = $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcontracts/:partyid/:masteraccountid/:customfilter/:expirationperiod/:includewarranty/:topdivision/:topregion',
                    {
                        partyid: '@partyid',
                        masteraccountid: '@masteraccountid',
                        customfilter: '@customfilter',
                        expirationperiod: '@expirationperiod',
                        includewarranty: '@includewarranty',
                        topdivision: '@topdivision',
                        topregion: '@topregion'
                    });

               // api/orcl/getcontracts/{partyid}/{masteraccountid}/{customfilter}/{expirationperiod}/{includewarranty}/{topdivision}/{topregion}
                return resource.query({
                    partyid: searchParams.CustomerID,
                    masteraccountid: searchParams.MasterAccountID,
                    customfilter: searchParams.filters,
                    expirationperiod: searchParams.expPeriod,
                    includewarranty: searchParams.warranty,
                    topdivision: searchParams.topDivisionId,
                    topregion: searchParams.topRegionId
                });                

            },

            generatecontract: function (searchParams) {
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/generatecontract/:headerid', { headerid: '@headerid' })
                    .get({ headerid: searchParams.headerId });
            },

            getAdditionalContracts: function (searchParams) {

                var resource;
                switch (searchParams.searchBy) {
                    case '1':
                        return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcontractsbynumber/:contractnumber', { contractnumber: '@contractnumber' }).
                        query({ contractnumber: searchParams.searchText });
                    case '2':
                        return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcontractsbysonumber/:sonumber', { sonumber: '@sonumber' }).
                         query({sonumber: searchParams.searchText });
                    case '3':
                        return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcontractsbyserialnumber/:serialnumber', { serialnumber: '@serialnumber' }).
                        query({ serialnumber: searchParams.searchText });
                    case '4':
                        return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcontractsbyponumber/:number', { number: '@number' }).
                        query({ number: searchParams.searchText });
                }

                return null;
            }
        };

    }]);





